package com.yiboshi.rehrs.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

public class WebUtils {

    public static String getHeaderValue(HttpServletRequest request, String header){
        String value = request.getHeader(header);
        return StringUtils.isEmpty(value)||"null".equalsIgnoreCase(value)?"":value;
    }

    public static String getHeaderVersionCode(HttpServletRequest request){
        return WebUtils.getHeaderValue(request!=null?request:getRequest(), "versionCode");
    }

    public static String getHeaderUA(HttpServletRequest request){
        return Optional.ofNullable(WebUtils.getHeaderValue(request!=null?request:getRequest(), "User-Agent")).orElse("");
    }
    public static String getIpAddress(){
        return getIpAddress(getRequest());
    }

    public static String getIpAddress(HttpServletRequest request) {
        if(request==null){
            request = getRequest();
        }
        if(request==null){
            return "";
        }
        String Xip = getHeaderValue(request,"X-Real-IP");
        String XFor =getHeaderValue(request,"X-Forwarded-For");
        if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = XFor.indexOf(",");
            if(index != -1){
                return XFor.substring(0,index);
            }else{
                return XFor;
            }
        }
        XFor = Xip;
        if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
            return XFor;
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getRemoteAddr();
        }
        return XFor;
    }

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes==null){
            return null;
        }
        HttpServletRequest request = requestAttributes.getRequest();
        return request;
    }

    public static HttpServletResponse getResponse() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes==null){
            return null;
        }
        HttpServletResponse response = requestAttributes.getResponse();
        return response;
    }
}
